import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:url_launcher/url_launcher.dart';

import './eventbus.dart';

class OptionMenuWidget extends StatelessWidget {
  const OptionMenuWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        _createMenu("assets/drawable/ic_discover_next_card_back.png",
            () => Fluttertoast.showToast(msg: "coming soon...😂😂")),
        _createMenu("assets/drawable/ic_discover_more.png",
            () => bus.emit("openCard", true)),
        _createMenu("assets/drawable/ic_discover_next_card_right.png", () {
          _showAboutDialog(context);
        }),
      ],
    );
  }

  Widget _createMenu(String picUrl, GestureTapCallback onTap) {
    return Expanded(
      child: Container(
        alignment: Alignment.center,
        child: GestureDetector(
          onTap: onTap,
          child: Image.asset(
            picUrl,
            width: 48,
            height: 48,
          ),
        ),
      ),
    );
  }

  _launchURL() async {
    const url = 'https://www.baidu.com';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

  _showAboutDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text("About"),
          content: const Text("Show me the code."),
          actions: <Widget>[
            TextButton(
              child: const Text("cancel"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: const Text("github"),
              onPressed: () {
                _launchURL();
                Navigator.of(context).pop();
              },
            )
          ],
        );
      },
    );
  }
}
